<!--
 * @Author: ૮₍ ˙О˙₎ა
 * @Date: 2023-08-28 20:03:32
-->
<template>
  <div>
    <el-form label-width="20px" inline>
      <el-form-item>
        <el-input placeholder="起始位置" v-model="start" />
      </el-form-item>
      <el-form-item>
        <el-input placeholder="目标位置" v-model="end" />
      </el-form-item>
      <el-form-item>
        <el-button @click="toDriving">开始导航</el-button>
      </el-form-item>
    </el-form>

    <baidu-map class="map" :center="point" zoom="15" :scroll-wheel-zoom="true">
      <bm-scale anchor="BMAP_ANCHOR_LEFT_RIGHT"></bm-scale>
      <bm-navigation anchor="BMAP_ANCHOR_TOP_RIGHT"></bm-navigation>
      <bm-map-type
        :map-types="['BMAP_NORMAL_MAP', 'BMAP_HYBRID_MAP']"
        anchor="BMAP_ANCHOR_TOP_LEFT"
      ></bm-map-type>

      <bm-marker
        :position="point"
        :dragging="true"
        animation="BMAP_ANIMATION_BOUNCE"
      >
        <bm-label
          content="你好"
          :labelStyle="{ color: 'red', fontSize: '24px' }"
          :offset="{ width: -35, height: 30 }"
        />
      </bm-marker>

      <bm-driving
        v-if="lushuPoint.start.lat"
        :start="lushuPoint.start"
        :end="lushuPoint.end"
        :auto-viewport="true"
        :panel="true"
        location="西安"
      ></bm-driving>
    </baidu-map>
  </div>
</template>

<script lang="ts" setup>
import { onMounted, ref } from 'vue'
import { useGeocoder } from 'vue-baidu-map-3x'

interface IpointData {
  lng: number
  lat: number
  we?: string
}

const lushuPoint = ref({
  start: {
    lng: 0,
    lat: 0,
  },
  end: {
    lng: 0,
    lat: 0,
  },
})

const start = ref('')
const end = ref('')
const toDriving = () => {
  useGeocoder().then((geocoder) => {
    geocoder.getPoint(start.value, (res: any) => {
      lushuPoint.value.start = res
    })
  })
  useGeocoder().then((geocoder) => {
    geocoder.getPoint(end.value, (res: any) => {
      lushuPoint.value.end = res
    })
  })
}

// 初始位置
const point = ref<IpointData>({
  lng: 116.404,
  lat: 39.915,
})

const getPoint = () => {
  useGeocoder().then((geocoder) => {
    geocoder.getPoint('陕西省西安市雁塔区赛格', (res: IpointData) => {
      point.value = res
    })
  })
}

onMounted(() => {
  getPoint()
})
</script>

<script lang="ts">
export default {
  name: 'MapsView',
}
</script>

<style>
.map {
  width: 100%;
  height: 600px;
}
</style>
